package com.yipingnai.blog.controller.admin;

import com.yipingnai.blog.model.Article;
import com.yipingnai.blog.model.RespBean;
import com.yipingnai.blog.model.RespPageBean;
import com.yipingnai.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: jiangym
 * @Date: 2020/5/21 15:49
 */
@RestController
@RequestMapping("/admin/article")
public class AdminArticleController {

    @Autowired
    ArticleService articleService;

    @GetMapping("/{id}")
    public RespBean getArticleById(@PathVariable("id") Integer id){
        Article article = articleService.getArticleById(id);
        if(article!=null){
            return RespBean.ok("获取成功",article);
        }
        return RespBean.error("获取失败");
    }

    @PostMapping("/")
    public RespBean addArticle(@RequestBody Article article){
        int result = articleService.addArticle(article);
        return result == 1 ? RespBean.ok("操作成功",article.getId()): RespBean.error("操作失败");
    }

    @GetMapping("/")
    public RespPageBean getArticleByState(@RequestParam(defaultValue = "1") Integer state, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer size){
        return articleService.getArticleByState(state,page,size);
    }

    @PutMapping("/")
    public RespBean updateArticltStateById(Integer id,Integer state){
        if(articleService.updateArticltStateById(id,state) > 0){
            return RespBean.ok("操作成功");
        }
        return RespBean.error("操作失败");
    }

    @DeleteMapping("/")
    public RespBean deleteArticleById(Integer id){
        if(articleService.deleteArticleById(id) > 0){
            return RespBean.ok("操作成功");
        }
        return RespBean.error("操作失败");
    }
}
